var camera, scene, renderer, controls;
var auto = false,temp=0;

var city = {
    //默认显示线
    type:"xian",
    //定时器
    dinshiqi:null,
    trace : [[[0.2,0.2,-40],[0.2,0.2,-11],[11,0.2,0],[0.2,0.2,11],[0.2,0.2,40]],
        [[0.2,0.2,-40],[0.2,0.2,-11],[10,0.2,-1],[40,0.2,-1]],
        [[0.2,0.2,-40],[0.2,0.2,-11],[12,0.2,1],[20,0.2,1],[20,0.2,40]],
        [[-0.4,0.2,-40],[-0.4,0.2,-11],[-11,0.2,0],[-0.4,0.2,11],[-0.4,0.2,40]],
        [[-0.4,0.2,-40],[-0.4,0.2,-11],[-11,0.2,0],[-0.4,0.2,11],[-0.4,0.2,14],[-15,0.2,14],[-15,0.2,40]]],
    //线组
    line:{},
    //线组
    points:{},
    //圆锥
    lind:{},
    light:[],
    //形状
    shape1:(function(){
        var pts = [];
        pts.push( new THREE.Vector2 (-1,-1));
        pts.push( new THREE.Vector2 (-1,1));
        pts.push( new THREE.Vector2 (1,1));
        pts.push( new THREE.Vector2 (1,-1));
        var shape = new THREE.Shape(pts);
        return shape;
    })(),
    //获取固定尺寸形状
    getShape1:function(len){
        var pts = [];
        pts.push( new THREE.Vector2 (-len,-len));
        pts.push( new THREE.Vector2 (-len,len));
        pts.push( new THREE.Vector2 (len,len));
        pts.push( new THREE.Vector2 (len,-len));
        var shape = new THREE.Shape(pts);
        return shape;
    },
    //获取固定尺寸形状
    getShape2:function(start,end){
        var pts = [];
        pts.push( new THREE.Vector2 (start[0],start[1]) );
        pts.push( new THREE.Vector2 (start[0],end[1]) );
        pts.push( new THREE.Vector2 (end[0],end[1]));
        pts.push( new THREE.Vector2 (end[0],start[1]) );
        var shape = new THREE.Shape( pts );
        return shape;
    },
    //获取固定尺寸
    getShape3:function(ptxy){
        var pts = [];
        for(var i=0;i<ptxy.length;i++){
            pts.push(new THREE.Vector2 (ptxy[i][0],ptxy[i][1]));
        }
        var shape = new THREE.Shape( pts );
        return shape;
    },
    //材质
    mater:(function(){
        var texture = new THREE.Texture( generateTexture() );
        texture.needsUpdate = true;

        new THREE.MeshLambertMaterial( { map: texture, transparent: true } )
        return [
            new THREE.MeshBasicMaterial( { color: "#ffffff", wireframe: true} ),//骨架
            new THREE.MeshPhongMaterial( { color: "#999900", specular: "#ffff00", shininess: 300, map: texture, transparent: true } ) ,//反光光源
//            new THREE.MeshNormalMaterial() ,//反光
//				new THREE.MeshPhongMaterial( { color: 0xdddddd, specular: 0x009900, shininess: 30, flatShading:true}),//反光
            new THREE.MeshBasicMaterial({color:"#ffffff",transparent:true,blending:THREE.AdditiveBlending}),//透明];
            new THREE.MeshLambertMaterial( { color: "#999999", wireframe: false }),//普通
            new THREE.MeshLambertMaterial( { map: texture, transparent: true } )//局部渲染
            ,new THREE.MeshLambertMaterial( { color: "#11ff18", wireframe: false })//普通
            ,new THREE.MeshStandardMaterial({roughness:1,color:"#11ff18",metalness:0} )//线
            ,new THREE.MeshBasicMaterial({color:"#27408B",transparent:true,blending:THREE.AdditiveBlending})//透明];
            ,new THREE.MeshBasicMaterial({color:"#010101",transparent:true,blending:THREE.AdditiveBlending})//透明];
            ,new THREE.MeshBasicMaterial({color:"#111111",transparent:true,blending:THREE.AdditiveBlending})//透明];

        ]
    })(),
    //愣锥材料
    setParam:function(dcolor){
        var data = [];
        for(var i=0;i<dcolor.gj.length;i++){//网格材质
            data.push(new THREE.MeshBasicMaterial( { color: dcolor.gj[i], wireframe: true}));
        }
        var data2 = [];
        for(var i=0;i<dcolor.tm.length;i++){//透明材质
//			data2.push(new THREE.MeshBasicMaterial({color:dcolor.tm[i],transparent:true,blending:THREE.AdditiveBlending}));
            data2.push(new THREE.MeshLambertMaterial( { color: dcolor.tm[i], wireframe: false }));
        }
        this.gj = data;
        this.tm = data2;
    },
    /**
     * 相机
     * @param dom
     */
    setCamera:function(dom){
        camera = new THREE.PerspectiveCamera( 40, dom.width()/dom.height(), 1, 2000 );
        camera.position.set(60,82,146)
//		camera.position.set(0,138,-1)
    },
    /**
     * 添加模型
     */
    addModules:function(){
        //=======================建筑
        city.addModule({shape:city.getShape2([-7,-12],[7,12]),position:[65,0,-50], height:30,mater:city.mater[3]});
        city.addModule({shape:city.getShape2([-7,-19],[7,19]),position:[-65,0,-50], height:60,mater:city.mater[3]});
        city.addModule({shape:city.getShape2([-7,-9],[7,9]),position:[-32,0,-50], height:20,mater:city.mater[3]});
        city.addModule({shape:city.getShape2([-7,-9],[7,9]),position:[-12,0,-50], height:23,mater:city.mater[3]});
        city.addModule({shape:city.getShape2([-7,-9],[7,9]),position:[18,0,-50], height:23,mater:city.mater[3]});
        city.addModule({shape:city.getShape2([-7,-9],[7,9]),position:[38,0,-50], height:33,mater:city.mater[3]});

        city.addModule({shape:city.getShape2([-3,-5],[3,5]),position:[-75,0,-30], height:23,mater:city.mater[3]});
        city.addModule({shape:city.getShape2([-3,-5],[3,5]),position:[-75,0,-20], height:23,mater:city.mater[3]});
        city.addModule({shape:city.getShape2([-3,-5],[3,5]),position:[-75,0,-10], height:23,mater:city.mater[3]});
        city.addModule({shape:city.getShape2([-3,-5],[3,5]),position:[-75,0,10], height:23,mater:city.mater[3]});
        city.addModule({shape:city.getShape2([-3,-5],[3,5]),position:[-75,0,20], height:23,mater:city.mater[3]});
        city.addModule({shape:city.getShape2([-3,-5],[3,5]),position:[-75,0,30], height:23,mater:city.mater[3]});

        city.addModule({shape:city.getShape2([-3,-5],[3,5]),position:[-55,0,-30], height:23,mater:city.mater[3]});
        city.addModule({shape:city.getShape2([-3,-5],[3,5]),position:[-55,0,-20], height:23,mater:city.mater[3]});
        city.addModule({shape:city.getShape2([-3,-5],[3,5]),position:[-55,0,-10], height:23,mater:city.mater[3]});
        city.addModule({shape:city.getShape2([-3,-5],[3,5]),position:[-55,0,10], height:23,mater:city.mater[3]});
        city.addModule({shape:city.getShape2([-3,-5],[3,5]),position:[-55,0,20], height:23,mater:city.mater[3]});
        city.addModule({shape:city.getShape2([-3,-5],[3,5]),position:[-55,0,30], height:23,mater:city.mater[3]});

        city.addModule({shape:city.getShape1(4),position:[-35,0,-35], height:12,mater:city.mater[3]});
        city.addModule({shape:city.getShape1(4),position:[-26,0,-35], height:14,mater:city.mater[3]});
        city.addModule({shape:city.getShape1(5),position:[-15,0,-34], height:16,mater:city.mater[3]});
        city.addModule({shape:city.getShape2([-5,-9],[5,9]),position:[18,0,-34], height:13,mater:city.mater[3]});
        city.addModule({shape:city.getShape2([-11,-5],[11,5]),position:[34,0,-28], height:22,mater:city.mater[3]});

        //中央建筑
        city.addModule({shape:city.getShape3([[0,-10],[-10,0],[0,10],[10,0]]),position:[0,0,0], height:1,mater:city.mater[8]});
        city.addModule({shape:city.getShape3([[-4,-4],[-4,4],[4,4],[4,-4]]),position:[0,3,0], height:6,mater:city.mater[3]});
        city.addModule({shape:city.getShape3([[-4,-4],[-4,4],[4,4],[4,-4]]),position:[0,3,0], height:6,mater:city.mater[0]});
        city.addModule2({shape:city.getShape3([[-3,-3],[-3,3],[3,3],[3,-3]]),position:[0,6,0], trace:[[0,9,0],[0,10,0]],mater:city.mater[8]});
        city.addModule2({shape:city.getShape3([[-3,-3],[-3,3],[3,3],[3,-3]]),position:[0,6,0], trace:[[0,10,0],[0,11,0]],mater:city.mater[3]});
        city.addModule2({shape:city.getShape3([[-3,-3],[-3,3],[3,3],[3,-3]]),position:[0,6,0], trace:[[0,11,0],[0,12,0]],mater:city.mater[8]});
        city.addModule2({shape:city.getShape3([[-3,-3],[-3,3],[3,3],[3,-3]]),position:[0,6,0], trace:[[0,12,0],[0,13,0]],mater:city.mater[3]});
        city.addModule2({shape:city.getShape3([[-3,-3],[-3,3],[3,3],[3,-3]]),position:[0,6,0], trace:[[0,13,0],[0,14,0]],mater:city.mater[8]});
        city.addModule2({shape:city.getShape3([[-3,-3],[-3,3],[3,3],[3,-3]]),position:[0,6,0], trace:[[0,14,0],[0,15,0]],mater:city.mater[3]});
        city.addModule2({shape:city.getShape3([[-3,-3],[-3,3],[3,3],[3,-3]]),position:[0,6,0], trace:[[0,15,0],[0,16,0]],mater:city.mater[8]});
        city.addModule2({shape:city.getShape3([[-3,-3],[-3,3],[3,3],[3,-3]]),position:[0,6,0], trace:[[0,16,0],[0,17,0]],mater:city.mater[3]});
        city.addModule2({shape:city.getShape3([[-3,-3],[-3,3],[3,3],[3,-3]]),position:[0,6,0], trace:[[0,17,0],[0,18,0]],mater:city.mater[8]});
        city.addModule2({shape:city.getShape3([[-3,-3],[-3,3],[3,3],[3,-3]]),position:[0,6,0], trace:[[0,18,0],[0,20,0]],mater:city.mater[3]});
        city.addModule2({shape:city.getShape3([[-0.5,-0.5],[-0.5,0.5],[0.5,0.5],[0.5,-0.5]]),trace:[[-2.6,19.5,-2.6],[-1,24,-1]],mater:city.mater[3]});
        city.addModule2({shape:city.getShape3([[-0.5,-0.5],[-0.5,0.5],[0.5,0.5],[0.5,-0.5]]),trace:[[2.6,19.5,-2.6],[1,24,-1]],mater:city.mater[3]});
        city.addModule2({shape:city.getShape3([[-0.5,-0.5],[-0.5,0.5],[0.5,0.5],[0.5,-0.5]]),trace:[[-2.6,19.5,2.6],[-1,24,1]],mater:city.mater[3]});
        city.addModule2({shape:city.getShape3([[-0.5,-0.5],[-0.5,0.5],[0.5,0.5],[0.5,-0.5]]),trace:[[2.6,19.5,2.6],[1,24,1]],mater:city.mater[3]});

        city.addModule2({shape:city.getShape3([[-10,2],[-12,2],[-12,15],[-2,15],[-2,10]]), trace:[[0,0,0],[0,1,0]],mater:city.mater[3]});

        city.addModule2({shape:city.getShape3([[-11,8],[-11,14],[-5,14],[-5,8]]), trace:[[0,1,0],[0,2,0]],mater:city.mater[3]});
        city.addModule2({shape:city.getShape3([[-11,8],[-11,14],[-5,14],[-5,8]]), trace:[[0,2,0],[0,3,0]],mater:city.mater[8]});
        city.addModule2({shape:city.getShape3([[-11,8],[-11,14],[-5,14],[-5,8]]), trace:[[0,3,0],[0,4,0]],mater:city.mater[3]});
        city.addModule2({shape:city.getShape3([[-11,8],[-11,14],[-5,14],[-5,8]]), trace:[[0,4,0],[0,5,0]],mater:city.mater[8]});
        city.addModule2({shape:city.getShape3([[-11,8],[-11,14],[-5,14],[-5,8]]), trace:[[0,5,0],[0,6,0]],mater:city.mater[3]});
        city.addModule2({shape:city.getShape3([[-11,8],[-11,14],[-5,14],[-5,8]]), trace:[[0,6,0],[0,7,0]],mater:city.mater[8]});
        city.addModule2({shape:city.getShape3([[-11,8],[-11,14],[-5,14],[-5,8]]), trace:[[0,7,0],[0,8,0]],mater:city.mater[3]});
        city.addModule2({shape:city.getShape3([[-11,8],[-11,14],[-5,14],[-5,8]]), trace:[[0,8,0],[0,9,0]],mater:city.mater[8]});

        city.addModule2({shape:city.getShape3([[-10,-2],[-12,-2],[-12,-15],[-2,-15],[-2,-10]]), trace:[[0,0,0],[0,1,0]],mater:city.mater[3]});
        city.addModule2({shape:city.getShape3([[-10,-2],[-12,-2],[-12,-15],[-2,-15],[-2,-10]]), trace:[[0,1,0],[0,2,0]],mater:city.mater[8]});
        city.addModule2({shape:city.getShape3([[-10,-2],[-12,-2],[-12,-15],[-2,-15],[-2,-10]]), trace:[[0,2,0],[0,3,0]],mater:city.mater[3]});
        city.addModule2({shape:city.getShape3([[-10,-2],[-12,-2],[-12,-15],[-2,-15],[-2,-10]]), trace:[[0,3,0],[0,4,0]],mater:city.mater[8]});
        city.addModule2({shape:city.getShape3([[-10,-2],[-12,-2],[-12,-15],[-2,-15],[-2,-10]]), trace:[[0,4,0],[0,5,0]],mater:city.mater[3]});

        city.addModule2({shape:city.getShape3([[10,-2],[23,-2],[23,-14],[26,-14],[26,-25],[20,-25],[2,-25],[2,-10]]), trace:[[0,0,0],[0,1,0]],mater:city.mater[3]});
        city.addModule2({shape:city.getShape3([[4,-15],[4,-24],[24,-24],[24,-15]]), trace:[[0,1,0],[0,15,0]],mater:city.mater[3]});
        city.addModule2({shape:city.getShape3([[11,-3],[22,-3],[22,-13],[11,-13]]), trace:[[0,1,0],[0,8,0]],mater:city.mater[3]});

        city.addModule2({shape:city.getShape3([[10,2],[18,2],[18,15],[8,15],[8,4]]), trace:[[0,0,0],[0,1,0]],mater:city.mater[3]});
        city.addModule2({shape:city.getShape3([[10,2],[18,2],[18,15],[8,15],[8,4]]), trace:[[0,1,0],[0,2,0]],mater:city.mater[8]});
        city.addModule2({shape:city.getShape3([[10,2],[18,2],[18,15],[8,15],[8,4]]), trace:[[0,2,0],[0,3,0]],mater:city.mater[3]});
        city.addModule2({shape:city.getShape3([[10,2],[18,2],[18,15],[8,15],[8,4]]), trace:[[0,3,0],[0,4,0]],mater:city.mater[8]});
        city.addModule2({shape:city.getShape3([[10,2],[18,2],[18,15],[8,15],[8,4]]), trace:[[0,4,0],[0,5,0]],mater:city.mater[3]});
        city.addModule2({shape:city.getShape3([[10,2],[18,2],[18,15],[8,15],[8,4]]), trace:[[0,5,0],[0,6,0]],mater:city.mater[8]});
        city.addModule2({shape:city.getShape3([[10,2],[18,2],[18,15],[8,15],[8,4]]), trace:[[0,6,0],[0,7,0]],mater:city.mater[3]});
        city.addModule2({shape:city.getShape3([[10,2],[18,2],[18,15],[8,15],[8,4]]), trace:[[0,7,0],[0,8,0]],mater:city.mater[8]});
        city.addModule2({shape:city.getShape3([[10,2],[18,2],[18,15],[8,15],[8,4]]), trace:[[0,8,0],[0,9,0]],mater:city.mater[3]});
        city.addModule2({shape:city.getShape3([[10,2],[18,2],[18,15],[8,15],[8,4]]), trace:[[0,9,0],[0,10,0]],mater:city.mater[8]});
        city.addModule2({shape:city.getShape3([[10,2],[18,2],[18,15],[8,15],[8,4]]), trace:[[0,10,0],[0,11,0]],mater:city.mater[3]});
        city.addModule2({shape:city.getShape3([[10,2],[18,2],[18,15],[8,15],[8,4]]), trace:[[0,11,0],[0,12,0]],mater:city.mater[8]});
        city.addModule2({shape:city.getShape3([[10,2],[18,2],[18,15],[8,15],[8,4]]), trace:[[0,12,0],[0,13,0]],mater:city.mater[3]});
        city.addModule2({shape:city.getShape3([[7,5],[7,16],[20,16],[20,25],[7,25],[1,25],[1,11]]), trace:[[0,0,0],[0,1,0]],mater:city.mater[8]});
        city.addModule2({shape:city.getShape3([[7,5],[7,16],[20,16],[20,25],[7,25],[1,25],[1,11]]), trace:[[0,1,0],[0,2,0]],mater:city.mater[8]});
        city.addModule2({shape:city.getShape3([[7,5],[7,16],[20,16],[20,25],[7,25],[1,25],[1,11]]), trace:[[0,2,0],[0,3,0]],mater:city.mater[8]});
        city.addModule2({shape:city.getShape3([[7,5],[7,16],[20,16],[20,25],[7,25],[1,25],[1,11]]), trace:[[0,3,0],[0,4,0]],mater:city.mater[8]});
        city.addModule2({shape:city.getShape3([[7,5],[7,16],[20,16],[20,25],[7,25],[1,25],[1,11]]), trace:[[0,4,0],[0,5,0]],mater:city.mater[8]});
        city.addModule2({shape:city.getShape3([[7,5],[7,16],[20,16],[20,25],[7,25],[1,25],[1,11]]), trace:[[0,5,0],[0,6,0]],mater:city.mater[8]});
        city.addModule2({shape:city.getShape3([[7,5],[7,16],[20,16],[20,25],[7,25],[1,25],[1,11]]), trace:[[0,6,0],[0,7,0]],mater:city.mater[3]});

        city.addModule2({shape:city.getShape3([[1,28],[30,28],[30,39],[1,39]]), trace:[[0,0,0],[0,15,0]],mater:city.mater[3]});

        city.addModule2({shape:city.getShape3([[-5,17],[-5,37],[-15,37],[-15,34],[-37,34],[-37,24],[-45,24],[-45,14],[-37,14],[-37,5],[-15,5],[-15,17]]), trace:[[0,0,0],[0,1,0]],mater:city.mater[3]});
        city.addModule2({shape:city.getShape3([[-5,17],[-5,37],[-15,37],[-15,34],[-37,34],[-37,24],[-45,24],[-45,14],[-37,14],[-37,5],[-15,5],[-15,17]]), trace:[[0,1,0],[0,10,0]],mater:city.mater[9]});
        city.addModule2({shape:city.getShape3([[-5,17],[-5,37],[-15,37],[-15,17]]), trace:[[0,10,0],[0,60,0]],mater:city.mater[8]});
        city.addModule2({shape:city.getShape3([[-15,24],[-15,34],[-27,34],[-27,24]]), trace:[[0,10,0],[0,60,0]],mater:city.mater[8]});
        city.addModule2({shape:city.getShape3([[-27,24],[-27,34],[-37,34],[-37,24]]), trace:[[0,10,0],[0,55,0]],mater:city.mater[8]});
        city.addModule2({shape:city.getShape3([[-15,14],[-15,24],[-45,24],[-45,14]]), trace:[[0,10,0],[0,70,0]],mater:city.mater[8]});
        city.addModule2({shape:city.getShape3([[-15,5],[-15,14],[-37,14],[-37,5]]), trace:[[0,10,0],[0,50,0]],mater:city.mater[8]});
        city.addModule2({shape:city.getShape3([[-5,-17],[-5,-37],[-15,-37],[-15,-17]]), trace:[[0,0,0],[0,20,0]],mater:city.mater[8]});

        var ss = [[-5,17],[-5,37],[-15,37],[-15,34],[-37,34],[-37,24],[-45,24],[-45,14],[-37,14],[-37,5],[-15,5],[-15,17]];
        var p = [];
        for(var i=0;i<ss.length;i++){
            p.push([0-ss[i][1],10,0-ss[i][0]]);
        }
        p.push(p[0]);
        city.addLine({color:"#777777",positions:p,type:"tps"});

        city.addModule2({shape:city.getShape3([[20,-50],[35,-50],[35,-80],[-20,-80],[-20,-70],[20,-70]]), trace:[[0,0,0],[0,15,0]],mater:city.mater[3]});
        city.addModule({shape:city.getShape2([-3,-7],[3,7]),position:[55,0,-10], height:10,mater:city.mater[3]});
        city.addModule({shape:city.getShape2([-3,-7],[3,7]),position:[55,0,0], height:10,mater:city.mater[3]});
        city.addModule({shape:city.getShape2([-3,-7],[3,7]),position:[55,0,10], height:10,mater:city.mater[3]});
        city.addModule({shape:city.getShape2([-3,-7],[3,7]),position:[55,0,20], height:10,mater:city.mater[3]});
        city.addModule({shape:city.getShape2([-3,-7],[3,7]),position:[55,0,30], height:10,mater:city.mater[3]});
        city.addModule({shape:city.getShape2([-3,-7],[3,7]),position:[55,0,40], height:10,mater:city.mater[3]});
        city.addModule({shape:city.getShape2([-3,-7],[3,7]),position:[55,0,50], height:10,mater:city.mater[3]});
        city.addModule({shape:city.getShape2([-3,-7],[3,7]),position:[55,0,60], height:10,mater:city.mater[3]});

        city.addModule({shape:city.getShape2([-7,-3],[7,3]),position:[25,0,50], height:10,mater:city.mater[3]});
        city.addModule({shape:city.getShape2([-7,-3],[7,3]),position:[25,0,30], height:10,mater:city.mater[3]});
        city.addModule({shape:city.getShape2([-7,-3],[7,3]),position:[15,0,50], height:10,mater:city.mater[3]});
        city.addModule({shape:city.getShape2([-7,-3],[7,3]),position:[15,0,30], height:10,mater:city.mater[3]});
        city.addModule({shape:city.getShape2([-7,-3],[7,3]),position:[35,0,50], height:10,mater:city.mater[3]});
        city.addModule({shape:city.getShape2([-7,-3],[7,3]),position:[35,0,30], height:10,mater:city.mater[3]});

        city.addModule({shape:city.getShape2([-7,-3],[7,3]),position:[25,0,70], height:10,mater:city.mater[3]});
        city.addModule({shape:city.getShape2([-7,-3],[7,3]),position:[15,0,70], height:10,mater:city.mater[3]});
        city.addModule({shape:city.getShape2([-7,-3],[7,3]),position:[35,0,70], height:10,mater:city.mater[3]});

        city.addModule({shape:city.getShape2([-7,-15],[7,15]),position:[-70,0,69], height:35,mater:city.mater[3]});

        city.addModule({shape:city.getShape2([-3,-5],[3,5]),position:[-35,0,50], height:8,mater:city.mater[3]});
        city.addModule({shape:city.getShape2([-3,-5],[3,5]),position:[-5,0,50], height:8,mater:city.mater[3]});
        city.addModule({shape:city.getShape2([-3,-5],[3,5]),position:[-35,0,60], height:8,mater:city.mater[3]});
        city.addModule({shape:city.getShape2([-3,-5],[3,5]),position:[-20,0,60], height:8,mater:city.mater[3]});
        city.addModule({shape:city.getShape2([-3,-5],[3,5]),position:[-5,0,60], height:8,mater:city.mater[3]});
        city.addModule({shape:city.getShape2([-3,-5],[3,5]),position:[-35,0,70], height:8,mater:city.mater[3]});
        city.addModule({shape:city.getShape2([-3,-5],[3,5]),position:[-20,0,70], height:8,mater:city.mater[3]});
        city.addModule({shape:city.getShape2([-3,-5],[3,5]),position:[-5,0,70], height:8,mater:city.mater[3]});
    },
    /**
     * 设置场景
     */
    setScene:function(){

        scene = new THREE.Scene();
        scene.add( new THREE.AmbientLight( "#010101") );//环境光源
//		scene.add( new THREE.AmbientLight( "#111111") );//环境光源
        light = new THREE.PointLight( "#222222" );//点光源
        light.position.copy( camera.position );
        light.position.setZ(500);
//		scene.add( light );

        var bulbGeometry = new THREE.SphereGeometry( 0.2, 16, 8 );
        for(var i=0;i<dcolor.light.length;i++){
            var bulbLight = new THREE.PointLight( dcolor.light[i], 10, 100, 2 );
            var bulbMat = new THREE.MeshStandardMaterial( {
                emissive: dcolor.light[i],
                emissiveIntensity: 1,
                color: dcolor.light[i]
            });
            bulbLight.add( new THREE.Mesh( i==0?new THREE.SphereGeometry( 0.0001, 16, 8 ):bulbGeometry, bulbMat ) );
            bulbLight.position.set( 0, 52, 0 );
            bulbLight.castShadow = true;
            scene.add( bulbLight );
            city.light.push(bulbLight);
        }


//		var bulbLight = new THREE.PointLight( "#222222", 10, 100, 2 );
//		var bulbMat = new THREE.MeshStandardMaterial( {
//			emissive: "#ffffff",
//			emissiveIntensity: 1,
//			color: "#ffffff"
//		});
//		bulbLight.add( new THREE.Mesh( bulbGeometry, bulbMat ) );
//		bulbLight.position.set( 30, 52, 0 );
//		bulbLight.castShadow = true;
//		scene.add( bulbLight );
//		city.light.push(bulbLight);
//
//		var bulbLight = new THREE.PointLight( "#220000", 10, 100, 2 );
//		var bulbMat = new THREE.MeshStandardMaterial( {
//			emissive: "#220000",//光源点颜色
//			emissiveIntensity: 1,
//			color: "#220000"
//		});
//		bulbLight.add( new THREE.Mesh( bulbGeometry, bulbMat ) );
//		bulbLight.position.set( 30, 52, 0 );
//		bulbLight.castShadow = true;
//		scene.add( bulbLight );
//		city.light.push(bulbLight);
    },
    //添加模型参数 形状 位置 高度 材质 尺寸：shape position height mater
    addModule:function(obj){
        var defaul = {shape:this.shape1,position:[0,0,0],height:1,mater:this.mater[0],scale:1};
        $.extend(defaul,obj);
        var start = defaul.position;
        var end = eval("("+JSON.stringify(start)+")");
        end[1] += defaul.height;
        var pointsa = [new THREE.Vector3(start[0],start[1],start[2]),new THREE.Vector3(end[0],end[1],end[2])];
        var randomSplinea =  new THREE.CatmullRomCurve3( pointsa );
        var geometrya = new THREE.ExtrudeGeometry(  defaul.shape, {
            steps			: 2,
            extrudePath		: randomSplinea
        });
        var linea = new THREE.Mesh( geometrya, defaul.mater );
        scene.add(linea);
    },
    //添加模型参数 形状 位置 高度 材质 尺寸：shape position trace[start,end] mater
    addModule2:function(obj){
        var defaul = {shape:this.shape1,trace:[[0,0,0],[0,1,0]],mater:this.mater[0],rotation:[0,0,0]};
        $.extend(defaul,obj);
        var start = defaul.trace[0];
        var end = defaul.trace[1];
        var pointsa = [new THREE.Vector3(start[0],start[1],start[2]),new THREE.Vector3(end[0],end[1],end[2])];
        var randomSplinea =  new THREE.CatmullRomCurve3( pointsa );
        var geometrya = new THREE.ExtrudeGeometry(  defaul.shape, {
            steps			: 2,
            extrudePath		: randomSplinea
        });
        var linea = new THREE.Mesh( geometrya, defaul.mater );
        scene.add(linea);
    },
    /**
     * 添加线 obj:color,positions
     */
    addLine:function(obj){
        var geometry_line = new THREE.Geometry();   //声明一个几何体
        var material_line = new THREE.LineBasicMaterial({ color: obj.color,linewidth: 1, });   //定义材质外观
        for(var i=0;i<obj.positions.length;i++){
            var p = obj.positions[i];
            var point = new THREE.Vector3(p[0],p[1],p[2]);
            geometry_line.vertices.push(point); //vertices属性里可以存放点对象
        }
        var line = new THREE.Line(geometry_line, material_line);    //定义一条线
        if(typeof city.line[obj.type] == "undefined"){
            city.line[obj.type] = [];
        }
        city.line[obj.type].push(line);
        scene.add(line);   //加入场景里
    },
    //添加点
    addPoint:function(obj){
        var defaul = {color:"#ffffff",position:[[-10,10,20],[-15,10,20]],type:"p"};
        $.extend(defaul,obj);
        var geometry = new THREE.Geometry();//声明一个空几何体对象
        var p2= new THREE.Vector3(-10,10,20);//顶点3坐标
        var p3 = new THREE.Vector3(-15,15,20);//顶点3坐标
        var list = defaul.position;
        for(var i=0;i<list.length;i++){
            p= new THREE.Vector3(list[i][0],list[i][1],list[i][2]);
            geometry.vertices.push(p); //顶点坐标添加到geometry对象
        }
        var material=new THREE.PointsMaterial({
            color:defaul.color,
            size:5//点对象像素尺寸
        });//材质对象
//	    points=new THREE.Points(geometry,city.mater[1]);//点模型对象
        points=new THREE.Points(geometry,material);//点模型对象
        if(typeof city.points[defaul.type] == "undefined"){
            city.points[defaul.type] = [];
        }
        city.points[defaul.type].push(points);
        scene.add(points);//点对象添加到场景中
    },
    //添加点线
    addPointLine:function(obj){
        var defaul = {color:"#ffffff",position:[[-10,10,20],[-15,10,20]],type:"p",lp:[-5,30,5]};
        $.extend(defaul,obj);
        this.addPoint(defaul);
        for(var i=0;i<defaul.position.length;i++){
            city.addLine({color:defaul.color,positions:[defaul.lp,defaul.position[i]],type:defaul.type});
        }
    },
    //添加棱锥
    addCylind:function(obj){
        var defaul = {t:0,b:3,h:10,num:4,position:[20,4,20],look:[10,0,0],type:"la",gj:0,tm:0};
        $.extend(defaul,obj);
        var geometry = new THREE.CylinderGeometry( defaul.t, defaul.b, defaul.h, defaul.num);
        geometry.rotateX( Math.PI / 2 );
        var meshz1 = new THREE.Mesh( geometry, city.tm[defaul.tm]);
//		var meshz1 = new THREE.Mesh( geometry, city.mater[5]);
        meshz1.position.set.apply(meshz1.position,defaul.position);
        scene.add( meshz1 );
        if(typeof city.lind[defaul.type] == "undefined"){
            city.lind[defaul.type] = [];
        }
        city.lind[defaul.type].push(meshz1);
        meshz1.lookAt( new THREE.Vector3(defaul.look[0],defaul.look[1],defaul.look[2]));

        var geometry = new THREE.CylinderGeometry( defaul.t, defaul.b, defaul.h, defaul.num);
        geometry.rotateX( Math.PI / 2 );
        var meshz1 = new THREE.Mesh( geometry, city.gj[defaul.gj]);
        meshz1.position.set.apply(meshz1.position,defaul.position);
        scene.add( meshz1 );
        if(typeof city.lind[defaul.type] == "undefined"){
            city.lind[defaul.type] = [];
        }
        city.lind[defaul.type].push(meshz1);
        meshz1.lookAt( new THREE.Vector3(defaul.look[0],defaul.look[1],defaul.look[2]));
    },
    /**
     * 添加文字
     * @param opt{} text,color,position
     */
    addText:function(opt){
        var loader = new THREE.FontLoader();
        loader.load(city.path+"helvetiker_bold.typeface.json", function (res) {
            var font = new THREE.TextBufferGeometry(opt.text, {
                font: res,
                size: 5,
                height: 0.01
            });
            font.computeBoundingBox(); // 运行以后设置font的boundingBox属性对象，如果不运行无法获得。
            font.computeVertexNormals();
            fontModel = new THREE.Mesh(font,city.mater[2]);
            fontModel.position.set.apply(fontModel.position,opt.position);
            //fontModel.rotation.x = - Math.PI / 2;
            scene.add(fontModel);
        });
    },
    /**
     * 拆分数组
     * @param source
     * @param size
     * @returns {Array}
     */
    splitData:function (source , size){
        var ind =[];
        var data = [];
        for(var i=0;i<source.length;i++){
            if(source[i][0] != null){
                ind.push(i);
                data.push(source[i]);
            }
        }
        var result = [];
        for(var i=0;i<size*(source.length-1);i++){
            result[i] = [null,null,null];
        }
        for(var i = 1;i<ind.length;i++){
            var start = data[i-1];
            var end = data[i];
            var num = (ind[i]-ind[i-1])*size;
            var jg_x = (end[0]-start[0])/num;
            var jg_y = (end[1]-start[1])/num;
            var jg_z = (end[2]-start[2])/num;
            for(var j=0;j<num;j++){
                result[ind[i-1]*size+j] = [start[0] +jg_x*j,start[1] +jg_y*j,start[2] +jg_z*j];
            }
        }
        //result[ind[i-1]*size] = data[i-1];
        return result;
    },
    //轨迹移动
    startMove:function(bulbLight,list){
        var data = city.splitData(list,100);
        var start=data[0],end;
        for(var i=0;i<data.length;i++){
            (function(index){
                setTimeout(function(){
                    bulbLight.position.set.apply(bulbLight.position,data[index]);
                    start = data[index];
                },index*50);
            })(i);
        }
//		city.startMove(city.light[0],[[0.2,0.2,-40],[0.2,0.2,-11],[11,0.2,0],[0.2,0.2,11],[0.2,0.2,40]])
//		city.startMove(city.light[1],[[-0.4,0.2,-40],[-0.4,0.2,-11],[-11,0.2,0],[-0.4,0.2,11],[-0.4,0.2,14],[-15,0.2,14],[-15,0.2,40]])
//		city.startMove(city.light[1],[[-15,40,20],[-15,0.2,20],[-15,40,20]])
    },
    //数据点移动(弃用)
    startPoint:function(){
        for(var i=0;i<728;i++){
            (function(index){
                setTimeout(function(){
                    scene.remove.apply(scene,city.line["p"]);
                    scene.remove.apply(scene,city.line["q"]);
                    scene.remove.apply(scene,city.line["r"]);
                    scene.remove.apply(scene,city.line["s"]);
                    var y1 = 5*Math.sin(index/100);
                    var y2 = -5*Math.sin(index/100);
                    var y3 = 3*Math.cos(index/100);
                    var y4 = -3*Math.cos(index/100);
                    city.points['p'][0].position.y = y2;
                    city.points['q'][0].position.y = y1;
                    city.points['q'][0].position.z = y3;
                    city.points['r'][0].position.y = y3;
                    city.points['s'][0].position.y = y4;
                    city.addLine({color:"#ff0000",positions:[[-5,30,5],[-10,10+y2,20]],type:"p"});
                    city.addLine({color:"#ff0000",positions:[[-5,30,5],[-12,10+y2,20]],type:"p"});
                    city.addLine({color:"#ff0000",positions:[[-5,30,5],[-15,10+y2,20]],type:"p"});
                    city.addLine({color:"#ff0000",positions:[[-5,30,5],[-10,10+y1,10+y3]],type:"q"});
                    city.addLine({color:"#ff0000",positions:[[-5,30,5],[-15,10+y1,9+y3]],type:"q"});

                    city.addLine({color:"#ffff00",positions:[[0,30,15],[-20,5+y3,20]],type:"r"});
                    city.addLine({color:"#ffff00",positions:[[0,30,15],[-20,15+y3,20]],type:"r"});
                    city.addLine({color:"#ffff00",positions:[[0,30,15],[-20,50+y3,20]],type:"r"});
                    city.addLine({color:"#ffff00",positions:[[0,30,15],[-15,5+y4,30]],type:"s"});
                    city.addLine({color:"#ffff00",positions:[[0,30,15],[-20,35+y4,30]],type:"s"});
                    city.addLine({color:"#ffff00",positions:[[0,30,15],[-20,45+y4,30]],type:"s"});

                    for(var k=0,len=city.lind.la.length;k<len;k++){
                        city.lind["la"][k].lookAt(new THREE.Vector3(y1/2-15,18,y3/2-15))
                    }
                    for(var k=0,len=city.lind.lb.length;k<len;k++){
                        city.lind["lb"][k].lookAt(new THREE.Vector3(y1/2-15,58,y3/2-15))
                    }
                },index*10);
            })(i);
        }
    },
    //自动播放
    autoPlay:function(){
        city.dinshiqi = setInterval(function(){
            var index = temp;
            var y1 = 5*Math.sin(index/100);
            var y2 = -5*Math.sin(index/100);
            var y3 = 3*Math.cos(index/100);
            var y4 = -3*Math.cos(index/100);
            if(city.type == "xian"){
                scene.remove.apply(scene,city.line["p"]);
                scene.remove.apply(scene,city.line["q"]);
                scene.remove.apply(scene,city.line["r"]);
                scene.remove.apply(scene,city.line["s"]);
                city.line.p = [];
                city.line.q = [];
                city.line.r = [];
                city.line.s = [];
                city.points['p'][0].position.y = y2;
                city.points['q'][0].position.y = y1;
                city.points['q'][0].position.z = y3;
                city.points['r'][0].position.y = y3;
                city.points['s'][0].position.y = y4;
                city.addLine({color:"#666666",positions:[[-5,30,5],[-10,10+y2,20]],type:"p"});
                city.addLine({color:"#666666",positions:[[-5,30,5],[-12,10+y2,20]],type:"p"});
                city.addLine({color:"#666666",positions:[[-5,30,5],[-15,10+y2,20]],type:"p"});
                city.addLine({color:"#666666",positions:[[-5,30,5],[-10,10+y1,10+y3]],type:"q"});
                city.addLine({color:"#666666",positions:[[-5,30,5],[-15,10+y1,9+y3]],type:"q"});
                city.addLine({color:"#666666",positions:[[-45,30,35],[-20,5+y3,20]],type:"r"});
                city.addLine({color:"#666666",positions:[[-45,30,35],[-20,15+y3,20]],type:"r"});
                city.addLine({color:"#666666",positions:[[-45,30,35],[-20,50+y3,20]],type:"r"});
                city.addLine({color:"#666666",positions:[[-45,30,35],[-15,5+y4,30]],type:"s"});
                city.addLine({color:"#666666",positions:[[-45,30,35],[-20,35+y4,30]],type:"s"});
                city.addLine({color:"#666666",positions:[[-45,30,35],[-20,45+y4,30]],type:"s"});
            }else{
                for(var k=0,len=city.lind.la.length;k<len;k++){
                    city.lind["la"][k].lookAt(new THREE.Vector3(y1/2-15,18,y3/2+15))
                }
                for(var k=0,len=city.lind.lb.length;k<len;k++){
                    city.lind["lb"][k].lookAt(new THREE.Vector3(y1/2-15,58,y3/2+15))
                }
            }



            if(temp %5000 == 0){
                city.startMove(city.light[0], [[50,30,50],[50,40,-50],[-50,30,-50],[-50,40,50],[50,30,50]]);
            }
//		    if(temp %4500 == 0){
//		    	city.startMove(city.light[1],city.trace[parseInt(city.trace.length*Math.random())]);
//		    }
//		    if(temp %4000 == 0){
//		    	city.startMove(city.light[2],city.trace[parseInt(city.trace.length*Math.random())])
//		    }
            temp ++;
        },10);
    },
    //初始化
    init:function(dom){
        var width = dom.width();
        var height = dom.height();
        init(dom);
        animate();
        function init(dom){
            city.setCamera(dom);
            city.setScene();
            //=======================地板
            var matStdParams = {
                roughness: 0.044676705160855, // calculated from shininess = 1000
                metalness: 0.0
            };
            var matStdFloor = new THREE.MeshStandardMaterial( matStdParams );
            var geoFloor = new THREE.BoxGeometry( 180, 0.1, 180 );
            var mshStdFloor = new THREE.Mesh( geoFloor, matStdFloor );
            scene.add( mshStdFloor );
            //=======================条形光源
            rectLight = new THREE.RectAreaLight( "#00F5FF", 500, 70, 20 );
            rectLight.position.set( 0, 0, 0 );
//			scene.add( rectLight );
            //=======================镜子
//			var groundMirror = new THREE.Mirror( 88, 88, {
//				clipBias: 0.003,
//				textureWidth: width * window.devicePixelRatio,
//				textureHeight: height * window.devicePixelRatio,
//				color: "#551A8B",
//				recursion: 1
//			} );
//			groundMirror.rotateX( - Math.PI / 2 );
//			groundMirror.position.y = 0.1;
//			scene.add( groundMirror );
            //=======================线
            var geometry_line = new THREE.Geometry();   //声明一个几何体
            var material_line = new THREE.LineBasicMaterial({ color: "#ffff00" });   //定义材质外观
            var p1_line = new THREE.Vector3( -40, 1, 0 );
            var p2_line = new THREE.Vector3( 40, 1, 0 );
            geometry_line.vertices.push(p1_line); //vertices属性里可以存放点对象
            geometry_line.vertices.push(p2_line);
            var line = new THREE.Line(geometry_line, material_line, THREE.LinePieces);    //定义一条线
//            scene.add(line);   //加入场景里


            var p1_line = new THREE.Vector3(0, 1, -40 );
            var p2_line = new THREE.Vector3(0, 1, 40 );
            geometry_line.vertices.push(p1_line); //vertices属性里可以存放点对象
            geometry_line.vertices.push(p2_line);
            var line = new THREE.Line(geometry_line, material_line, THREE.LinePieces);    //定义一条线
//            scene.add(line);   //加入场景里

            if(city.type == "xian"){
                //点
//			city.addPoint( {color:"#ff0000",position:[[-10,10,20],[-15,10,20]],type:"p"});
                city.addPointLine({color:"#8B008B",position:[[-10,10,20],[-15,10,20],[-12,10,20]],type:"p",lp:[-5,30,5]});
                city.addPointLine({color:"#9400D3",position:[[-10,10,10],[-15,10,9]],type:"q",lp:[-5,30,5]});

                city.addPointLine({color:"#ffffff",position:[[-20,5,20],[-20,15,20],[-20,50,20]],type:"r",lp:[-45,30,35]});
                city.addPointLine({color:"#ffffff",position:[[-15,5,30],[-20,35,30],[-20,45,30]],type:"s",lp:[-45,30,35]});
                //字体
                city.addText({text:"5",color:"#ff0000",position:[-5,30,5]});
                city.addText({text:"6",color:"#ffff00",position:[-50,30,35]});
            }else{
                //三棱锥
                city.addCylind({t:4,b:0,h:16,num:4,position:[-20,24,20],look:[15,18,15],type:"la",gj:0,tm:0});
                city.addCylind({t:0,b:2,h:12,num:4,position:[-10,24,10],look:[15,18,15],type:"la",gj:0,tm:0});
                city.addCylind({t:0,b:2,h:8,num:5,position:[-20,24,10],look:[15,18,15],type:"la",gj:0,tm:0});
                city.addCylind({t:0,b:2,h:7,num:4,position:[-10,24,20],look:[15,18,15],type:"la"});
                city.addCylind({t:0,b:2,h:10,num:5,position:[-20,34,20],look:[15,18,15],type:"la"});

                city.addCylind({t:0,b:2,h:12,num:4,position:[-15,34,10],look:[-15,58,-15],type:"lb"});
                city.addCylind({t:0,b:3,h:11,num:4,position:[-25,34,10],look:[-15,58,-15],type:"lb"});
                city.addCylind({t:0,b:2,h:14,num:4,position:[-35,34,20],look:[-15,58,-15],type:"lb"});
            }

            //轨迹
            for(var i=0;i<city.trace.length;i++){
//				city.addLine({color:"#cccccc",positions:city.trace[i],type:"tp"});
            }
            //======================球
            var sphereGeometry = new THREE.SphereGeometry( 3, 20, 20 );
            sphere = new THREE.Mesh( sphereGeometry, city.mater[3] );
            sphere.position.set( 0, 26, 0 );
            scene.add( sphere );
            //======================圆柱
            var arcShape = new THREE.Shape();
            arcShape.moveTo( 50, 60 );
            arcShape.absarc( 0, 0, 6, 0, Math.PI * 2, false );
            var extrudeSettings = { amount: 1, bevelEnabled: true, bevelSegments: 2, steps: 2, bevelSize: 1, bevelThickness: 1 };
            var geometry = new THREE.ExtrudeGeometry( arcShape, extrudeSettings );
            meshcycle = new THREE.Mesh( geometry, city.mater[3] );
            meshcycle.rotation.x = -Math.PI / 2;
            meshcycle.position.set( 0, 1, 0 );
            //mesh.rotation.set( rx, ry, rz );
            //mesh.scale.set( 1, 1, 1 );
            scene.add( meshcycle );
            //======================圆柱2
            var arcShape = new THREE.Shape();
            arcShape.moveTo( 50, 60 );
            arcShape.absarc( 0, 0, 15, 0, Math.PI * 2, false );
            var extrudeSettings = { amount: 20, bevelEnabled: true, bevelSegments: 2, steps: 2, bevelSize: 1, bevelThickness: 1 };
            var geometry = new THREE.ExtrudeGeometry( arcShape, extrudeSettings );
            meshcycle = new THREE.Mesh( geometry, city.mater[3] );
            meshcycle.rotation.x = -Math.PI / 2;
            meshcycle.position.set( -60, 1, 60 );
            scene.add( meshcycle );
            
            
            
           

			material = new THREE.MeshLambertMaterial( {
				color: 0x000360,
				side: THREE.DoubleSide
			} );
			object = new THREE.Mesh( new THREE.CylinderBufferGeometry( 2, 5, 90, 4, 0 ), material );
			object.position.set( 50, 0, - 40 );
			scene.add( object );

			
			
			
            //======================三棱锥
            geometryobj = new THREE.CylinderGeometry( 0, 2, 10, 10);
//			geometryobj.rotateX( Math.PI / 2 );
            meshobj = new THREE.Mesh( geometryobj, city.mater[0] );
            meshobj.position.set(0,35,0);
//			meshobj.updateMatrix();
//			meshobj.matrixAutoUpdate = false;
//			meshobj.rotateX( Math.PI / 2 );
           // scene.add( meshobj );

            //=======================scene
            renderer = new THREE.WebGLRenderer( { antialias: true,alpha:true} );
            renderer.setPixelRatio( window.devicePixelRatio );
            renderer.setSize( width,height);
            dom[0].appendChild( renderer.domElement );
            renderer.gammaInput = true;
            renderer.gammaOutput = true;
            renderer.setClearColor(dcolor.scene );
            controls = new THREE.OrbitControls( camera );
            window.addEventListener( 'resize', onWindowResize, false );
        }

        function onWindowResize() {
            camera.aspect = width/height;
            camera.updateProjectionMatrix();
            renderer.setSize( width,height );
        }
        function animate() {
            requestAnimationFrame( animate );
            if(auto){
                scene.rotation.set(0,scene.rotation._y+0.01,0);
            }
            renderer.render( scene, camera );
        }
    }
};
//材质相关
function generateTexture() {
    var canvas = document.createElement( 'canvas' );
    canvas.width = 256;
    canvas.height = 256;
    var context = canvas.getContext( '2d' );
    var image = context.getImageData( 0, 0, 206, 206 );
//	var image = context.getImageData( 200,206, 106, 206 );
    var x = 0, y = 0;
    for ( var i = 0, j = 0, l = image.data.length; i < l; i += 4, j ++ ) {
        x = j % 256;
        y = x == 0 ? y + 1 : y;
        image.data[ i ] = 255;
        image.data[ i + 1 ] = 255;
        image.data[ i + 2 ] = 255;
        image.data[ i + 3 ] = Math.floor( x ^ y );
    }
    context.putImageData( image, 0, 0 );
    return canvas;
}